<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }

        #box{
            width: 500px;
            height: 350px;
            margin: 0 auto;
            margin-top: 2em;
            border: 1px solid rebeccapurple;
            overflow: hidden;
        }

        #tab{
            width: 100%;
            height: 35px;
            display: flex;
        }

        #tab .item{
            width: 25%;
            text-align: center;
            line-height: 35px;
            border-right: 1px solid rebeccapurple;
            border-bottom: 1px solid rebeccapurple;
        }

        #tab .item:last-child{
            border-right: none;
        }

        #content,#content .item{
            width: 100%;
            height: 100%;
        }

        #content .item{
            text-align: center;
            line-height: 350px;
            display: none;
        }

        #tab .item.active{
            background-color: red;
            border-bottom:none;
            color: #fff;
        }

        #content .item.active{
            background-color: red;
            color: #fff;
            display: block;
        }
    </style>
</head>
<body>
    <div id="box">
        <div id="tab">
            <div class="item active">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
        </div>
        <div id="content">
            <div class="item active">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
        </div>
    </div>
</body>
</html>

<script>
    document.getElementById('tab').addEventListener('click', function(event) {
        if (event.target.classList.contains('item')) {
            let index = Array.from(event.target.parentNode.children).indexOf(event.target);
            let tabItems = document.querySelectorAll('#tab .item');
            let contentItems = document.querySelectorAll('#content .item');

            tabItems.forEach(item => item.classList.remove('active'));
            contentItems.forEach(item => item.classList.remove('active'));

            tabItems[index].classList.add('active');
            contentItems[index].classList.add('active');
        }
    });
</script>